﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

using System;
using System.IO;
using System.Reflection;
using System.Resources;

namespace Tesla.Content {
    /// <summary>
    /// Resource locator for finding resources embedded in an assembly.
    /// </summary>
    public class EmbeddedResourceLocator : IResourceLocator {
        private ResourceManager _resourceManager;

        /// <summary>
        /// Gets the root name of the resource files.
        /// </summary>
        public String RootDirectory {
            get {
                return _resourceManager.BaseName;
            }
        }

        /// <summary>
        /// Creates a new embedded resource locator with specified root name and assembly.
        /// </summary>
        /// <param name="rootName">Root name of the resources</param>
        /// <param name="assembly">Assembly where the resources are embedded</param>
        public EmbeddedResourceLocator(String rootName, Assembly assembly) {
            _resourceManager = new ResourceManager(rootName, assembly);
        }

        /// <summary>
        /// Creates a new embedded resource locator with specified resource manager.
        /// </summary>
        /// <param name="resourceManager">Resource manager</param>
        public EmbeddedResourceLocator(ResourceManager resourceManager) {
            _resourceManager = resourceManager;
        }

        /// <summary>
        /// Locates a resource from the specified asset name.
        /// </summary>
        /// <param name="fileName">Asset name, including extension</param>
        /// <returns>Resource, if it exists or null</returns>
        /// <exception cref="System.NullReferenceException">Thrown if filename is null.</exception>
        public IResource LocateResource(String fileName) {
            if(String.IsNullOrEmpty(fileName)) {
                throw new NullReferenceException(String.Format("File {0} string cannot be null", fileName));
            }

            if(_resourceManager.GetObject(fileName) == null) {
                return new EmbeddedResource(_resourceManager, fileName);
            } else {
                return null;
            }
        }

        /// <summary>
        /// Gets the fully qualified path of the asset.
        /// </summary>
        /// <param name="assetName">Asset name</param>
        /// <returns>Fully qualified path</returns>
        public String GetFullPath(String assetName) {
            return String.Concat(RootDirectory, ".", Path.GetFileNameWithoutExtension(assetName));
        }

        /// <summary>
        /// Gets the full path relative to.
        /// </summary>
        /// <param name="assetName">Name of the asset.</param>
        /// <param name="resource">The resource.</param>
        /// <returns></returns>
        public String GetFullPathRelativeTo(String assetName, IResource resource) {
            if(!(resource is EmbeddedResource)) {
                return String.Empty;
            }

            return GetFullPath(assetName);
        }
    }
}
